package pkg

import (
	_ "github.com/mbobakov/grpc-consul-resolver" // It's important
	"go.uber.org/zap"
	"goods_bff/goods_proto"
	"google.golang.org/grpc"
)

//	func Comment() goods_proto.UserClient {
//		dial, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
//		if err != nil {
//			zap.S().Panic(err)
//			return nil
//		}
//		//defer dial.Close()
//		client := goods_proto.NewUserClient(dial)
//		return client
//	}

//func Comment() goods_proto.UserClient {
//	config := api.DefaultConfig()
//	config.Address = fmt.Sprintf("%s:%d", "127.0.0.1", 8500)
//	newClient, err := api.NewClient(config)
//	if err != nil {
//		zap.S().Panic(err)
//	}
//	filter, err := newClient.Agent().ServicesWithFilter("Service == user_srv")
//	if err != nil {
//		zap.S().Panic(err)
//	}
//	var address string
//	var port int
//	for _, v := range filter {
//		address = v.Address
//		port = v.Port
//	}
//	dial, err := grpc.Dial(fmt.Sprintf("%s:%d", address, port), grpc.WithInsecure())
//	if err != nil {
//		zap.S().Panic(err)
//		return nil
//	}
//	//defer dial.Close()
//	client := goods_proto.NewUserClient(dial)
//	return client
//}

func Comment() goods_proto.GoodsClient {
	// 负载均衡策略 轮询策略
	//consul的tag必须对上
	//_ "github.com/mbobakov/grpc-consul-resolver" // It's important 必须引入
	//调用用户微服务的rpc注册方法
	dial, err := grpc.Dial("consul://127.0.0.1:8500/goods_srv?wait=14s",
		grpc.WithInsecure(),
		//负载均衡策略 轮询策略
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.S().Panic(err)
	}
	client := goods_proto.NewGoodsClient(dial)
	return client
}
